﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Menus;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using System;
using System.Security.Cryptography;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Menus
{
    public class GetMenuCommandHandler : IRequestHandler<GetMenuCommand, APIResult<List<MenuDTO>>>
    {
        private readonly IBaseRepository<Menu> menuRep;

        public GetMenuCommandHandler(IBaseRepository<Menu> menuRep)
        {
            this.menuRep = menuRep;
        }
        
        public async Task<APIResult<List<MenuDTO>>> Handle(GetMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MenuDTO>> result = new APIResult<List<MenuDTO>>();


            
            var list = from a in await menuRep.GetAll()
                       join b in await menuRep.GetAll() on a.MenuId equals b.PId
                       select new MenuDTO 
                       {
                           MenuId=b.MenuId,
                           MenuName=b.MenuName,
                           Url=b.Url,
                           PId = b.PId,
                           CreateName=b.CreateName,
                           CreateTime = b.CreateTime,
                           UpdateName = b.UpdateName,
                           UpdateTime = b.UpdateTime,
                           OrderById = b.OrderById,
                           ParendName=a.MenuName
                       };
            list=list.OrderByDescending(x => x.MenuId);
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data=list.ToList();
            return await Task.FromResult(result);
        }
    }
}
